﻿/****
 * 
 * Code provenant de l'outils Bowis de codeplex
 * Merci !!!
 * 
 * *******/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Client;
using System.Xml;

namespace WordTemplate1.Formulaire
{
    public partial class AreaPathIterationForm : Form
    {
        TfsTeamProjectCollection server;
        ICommonStructureService css;
        string projectName;

        public AreaPathIterationForm()
        {
            InitializeComponent();
            this.projectName = TFS.project.Name;
            this.server = TFS.tfsTP;
            this.lblTeamProjectCollection.Text = server.Name;
            this.lblTeamProjectName.Text = this.projectName;
        }

        private void AreaPathIterationForm_Load(object sender, EventArgs e)
        {
            css = (ICommonStructureService)server.GetService(typeof(ICommonStructureService));

            ProjectInfo projectInfo = css.GetProjectFromName(projectName);

            NodeInfo[] nodes = css.ListStructures(projectInfo.Uri);

            XmlElement AreaTree = css.GetNodesXml(new string[] { nodes[1].Uri }, true);

            XmlElement IterationsTree = css.GetNodesXml(new string[] { nodes[0].Uri }, true);

            tvArea.Nodes.Clear();
            tvIteration.Nodes.Clear();

            var areaNode = tvArea.Nodes.Add("Area");
            var iterationNode = tvIteration.Nodes.Add("Iteration");
            areaNode.Tag = nodes[1].Uri;
            iterationNode.Tag = nodes[0].Uri;

            XmlNode AreaNodes = AreaTree.ChildNodes[0];
            XmlNode IterationsNodes = IterationsTree.ChildNodes[0];

            MakeList(areaNode, AreaNodes, StructureType.Area);

            MakeList(iterationNode, IterationsNodes, StructureType.Iteration);

            tvArea.ExpandAll();
            tvIteration.ExpandAll();
        }

        enum StructureType
        {
            Iteration = 0,
            Area = 1
        }

        private void MakeList(TreeNode parentNode, XmlNode tree, StructureType type)
        {
            if (tree.FirstChild != null)
            {
                int myNodeCount = tree.FirstChild.ChildNodes.Count;

                for (int i = 0; i < myNodeCount; i++)
                {
                    XmlNode Node = tree.ChildNodes[0].ChildNodes[i];
                    TreeNode newNode = null;
                    if (type == StructureType.Area)
                        newNode = parentNode.Nodes.Add(Node.Attributes["Name"].Value);
                    else
                        newNode = parentNode.Nodes.Add(Node.Attributes["Name"].Value);

                    newNode.Tag = Node.Attributes["NodeID"].Value;
                    if (Node.ChildNodes != null && Node.ChildNodes.Count > 0)
                        MakeList(newNode, Node, type);
                }
            }
        }

        TreeNode editingNode;

        private void btnAddIteration_Click(object sender, EventArgs e)
        {
            editingNode = tvIteration.SelectedNode.Nodes.Add("Iteration N");
            tvIteration.SelectedNode = editingNode;
            editingNode.BeginEdit();
        }

        private void TreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            e.Node.EndEdit(false);
            e.Node.Text = e.Label;
            if (e.Node.Tag != null)
                css.RenameNode(e.Node.Tag.ToString(), e.Label);
            else
            {
                string uri = css.CreateNode(e.Node.Text, e.Node.Parent.Tag.ToString());
                e.Node.Tag = uri;
            }
        }

        private void btnDeleteIteration_Click(object sender, EventArgs e)
        {
            DeleteSelectedNode(tvIteration);
        }

        private void btnUpIteration_Click(object sender, EventArgs e)
        {
            UpSelectedNode(tvIteration);
        }

        private void btnDownIteration_Click(object sender, EventArgs e)
        {
            DownSelectedNode(tvIteration);
        }

        private void tvIteration_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByKeyboard || e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.Unknown)
            {
                btnDownIteration.Enabled = e.Node != null && e.Node.Parent != null && e.Node.NextNode != null;
                btnUpIteration.Enabled = e.Node != null && e.Node.Parent != null && e.Node.PrevNode != null;
                btnDeleteIteration.Enabled = e.Node != null && e.Node.Parent != null;
                btnLeftIteration.Enabled = e.Node != null && e.Node.Parent != null && e.Node.Parent.Parent != null;
                btnRightIteration.Enabled = e.Node != null && e.Node.Parent != null && e.Node.PrevNode != null;
            }
        }

        private void tvArea_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByKeyboard || e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.Unknown)
            {
                btnDownArea.Enabled = e.Node != null && e.Node.Parent != null && e.Node.NextNode != null;
                btnUpArea.Enabled = e.Node != null && e.Node.Parent != null && e.Node.PrevNode != null;
                btnDeleteArea.Enabled = e.Node != null && e.Node.Parent != null;
                btnLeftArea.Enabled = e.Node != null && e.Node.Parent != null && e.Node.Parent.Parent != null;
                btnRightArea.Enabled = e.Node != null && e.Node.Parent != null && e.Node.PrevNode != null;
            }
        }

        private void btnAddArea_Click(object sender, EventArgs e)
        {
            editingNode = tvArea.SelectedNode.Nodes.Add("Area N");
            tvArea.SelectedNode = editingNode;
            editingNode.BeginEdit();
        }

        private void btnDeleteArea_Click(object sender, EventArgs e)
        {
            DeleteSelectedNode(tvArea);
        }

        private void btnUpArea_Click(object sender, EventArgs e)
        {
            UpSelectedNode(tvArea);
        }

        private void btnDownArea_Click(object sender, EventArgs e)
        {
            DownSelectedNode(tvArea);
        }

        private void btnLeftArea_Click(object sender, EventArgs e)
        {
            MoveLeftSelectedNode(tvArea);
        }

        private void btnRightArea_Click(object sender, EventArgs e)
        {
            MoveRightSelectedNode(tvArea);
        }

        private void btnLeftIteration_Click(object sender, EventArgs e)
        {
            MoveLeftSelectedNode(tvIteration);
        }

        private void btnRightIteration_Click(object sender, EventArgs e)
        {
            MoveRightSelectedNode(tvIteration);
        }

        private void tvIteration_KeyUp(object sender, KeyEventArgs e)
        {
            TreeView treeview = tvIteration;
            if (e.KeyCode == Keys.F2 && treeview.SelectedNode != null && treeview.SelectedNode.Parent != null)
            {
                treeview.SelectedNode.BeginEdit();
            }
        }

        private void tvArea_KeyUp(object sender, KeyEventArgs e)
        {
            TreeView treeview = tvArea;
            if (e.KeyCode == Keys.F2 && treeview.SelectedNode != null && treeview.SelectedNode.Parent != null)
            {
                treeview.SelectedNode.BeginEdit();
            }
        }

        private void MoveLeftSelectedNode(TreeView treeview)
        {
            TreeNode currentNode = treeview.SelectedNode;
            if (currentNode != null && currentNode.Parent != null && currentNode.Parent.Parent != null)
            {
                css.MoveBranch(currentNode.Tag.ToString(), currentNode.Parent.Parent.Tag.ToString());
                TreeNode newParent = currentNode.Parent.Parent;
                int newIndex = currentNode.Parent.Index + 1;
                currentNode.Remove();
                newParent.Nodes.Add(currentNode);
                treeview.SelectedNode = currentNode;
            }
        }

        private void MoveRightSelectedNode(TreeView treeview)
        {
            TreeNode currentNode = treeview.SelectedNode;
            if (currentNode != null && currentNode.Parent != null && currentNode.PrevNode != null)
            {
                css.MoveBranch(currentNode.Tag.ToString(), currentNode.PrevNode.Tag.ToString());
                TreeNode newParent = currentNode.PrevNode;
                currentNode.Remove();
                newParent.Nodes.Add(currentNode);
                treeview.SelectedNode = currentNode;
            }
        }

        private void UpSelectedNode(TreeView treeview)
        {
            TreeNode currentNode = treeview.SelectedNode;

            if (currentNode != null && currentNode.Parent != null && currentNode.PrevNode != null)
            {
                css.ReorderNode(currentNode.Tag.ToString(), -1);
                TreeNode parent = currentNode.Parent;
                int newIndex = currentNode.Index - 1;
                parent.Nodes.Remove(currentNode);
                parent.Nodes.Insert(newIndex, currentNode);
                treeview.SelectedNode = currentNode;
            }
        }

        private void DownSelectedNode(TreeView treeview)
        {
            TreeNode currentNode = treeview.SelectedNode;

            if (currentNode != null && currentNode.Parent != null && currentNode.NextNode != null)
            {
                css.ReorderNode(currentNode.Tag.ToString(), +1);
                TreeNode parent = currentNode.Parent;
                int newIndex = currentNode.Index + 1;
                parent.Nodes.Remove(currentNode);
                parent.Nodes.Insert(newIndex, currentNode);
                treeview.SelectedNode = currentNode;
            }
        }

        private void DeleteSelectedNode(TreeView treeview)
        {
            TreeNode node = treeview.SelectedNode;
            if (node != null && node.Parent != null)
            {
                string reclassifyUri = "";
                if (node.Parent.Tag != null)
                    reclassifyUri = node.Parent.Tag.ToString();
                css.DeleteBranches(new string[] { node.Tag.ToString() }, reclassifyUri);

                treeview.Nodes.Remove(node);
            }
        }
    }
}
